﻿/// <reference path="JQUERY/jquery-1.10.2.js" />
/// <reference path="/scripts/jquery/jquery-1.7.2.min.js" />
var clientID = "";
var bt_search, bt_orderBy, bt_page, bt_bat, tb_batIDstr, tb_currentPage, tb_orderBy, tb_descasc;
var FormCkeditor;
var isMobile = false;
var kk = 1;
//
var trH1 = 37, trH2 = 57, trH3 = 77;
var trHeight = trH1;
var bodyWidth = 454;
//var Element_InRight;
//
var showSearch = false;


var ListSumColumn;
//

//ListShow
var liWidth = 150, liHeight = 150;

$(function () {
    document.ontouchmove = function (e) {
        if (document.body.scrollHeight > parent.document.getElementById("admin_list").scrollHeight) return;
        e.preventDefault();
    }

    //
    var _se = document.createElement("link");
    _se.type = "text/css";
    _se.rel = "stylesheet";
    var _st = "T";
    if (parent.screen.width < 768) {
        _st = "M";
        isMobile = true;
    }
    _se.href = "/css/List_" + _st + ".css";
    //document.getElementsByTagName("head")[0].appendChild(_se);
    $("body").addClass(_st);
    if (!isMobile) $("body").width(bodyWidth);


    $("#ListTitle").html(document.title);

    default_bt_goBack();//初始化返回按钮事件
    makeClientID();

    $("#bt_goBack").fastClick(function () {
        parent.frameList_Goback();
    });
    //$("[data-toggle=tooltip]").mouseover(function () { $(this).tooltip('show');})

    make_pageSize();//自动pageSize
    page_build("");//构造翻页

    //初始化搜索按钮事件
    $("#button_search").click(function () { $("[name='" + clientID + "$tb_currentPage']").val(0); $("[name='" + clientID + "$bt_search']").click(); });
    $(".search_onchange").change(function () { $("#button_search").click(); })
    $(".button_bat").fastClick(function () { button_bat_click(this); return false; })
    orderBy_default(); //初始化 orderby事件

    bt_state_default();//初始化 修改状态 按钮 dom+事件
    bt_move_default();//初始化 上下移动 按钮 dom+事件
    //ajax updatepanel 重载调用的函数
    try {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
            //$("#ListShowUl li").width(liWidth).height(liHeight);
            //$("#List").addClass("trH" + trHeight);
            //orderBy_default();
            //bt_state_default();
            //bt_move_default();
            //checkbox_event();
            //chk_bat();

        });
    } catch (e) { }

    //
    bindEvent_href();
    //
    // parent.frame_list_loadOK($(window.document).width());
    if ($(".formTable .button_submit").length > 0) {
        $(".formTable .button_submit").fastClick(function () {
            $(this).addClass("button_disabled").attr("disabled");
        })
    }
    //
   // parent.showMenu(location.pathname.toLowerCase());
    //parent.showMenu(location.href.toLowerCase());
    //
    try {
        document.addEventListener("touchstart", function (event) { document_click($(event.target)); }, false);

    } catch (e) { }
    // $(document).fastClick(function () { document_click(event); })

    addListener(document, "click", function (evt) {
        var evt = window.event ? window.event : evt, eventTarget = evt.srcElement || evt.target;
        document_click($(eventTarget));
    })

    try { page_load_ok(); }
    catch (e) { }
    if ($("#div_search_con .control-group").length > 0 && $("#div_search_con").html() != "") {
        parent.showSearchOpener();
        //$("#search_opener").show();
        //$("#Top_Control_Box").addClass("hasSearch");
    } else parent.hideSearchOpener();
    //
    var Top_Control_Box_buttons = $("#Top_Control_Box");
    $("#Top_Control_Box button").each(function () {
        $(this).attr("href-right", getAbsoluteUrl($(this).attr("href-right")));
    });

    //Top_Control_Box_buttons.each(function () {
    //    $(this).attr("href-right", getAbsoluteUrl($(this).attr("href-right")));
    //})
    //
    //var topButton = $("#Top_Control_Box button");
    //topButton.each(function () {
    //    $(this).attr("title", "wori");
    //    this.title = $(this).attr("title");
    //})
    //alert(topButton.outerHTML());
    top.Build_Top_Control_Box($("#Top_Control_Box"));

})
function addListener(element, e, fn) {
    if (element.addEventListener) {
        element.addEventListener(e, fn, false);
    } else {
        element.attachEvent("on" + e, fn);
    }
}
function document_click(eventTarget) {
    if (isMobile) parent.Show_admin_list_M();
    //var eventTarget = $(event.target);
    if (eventTarget.closest(".td_controls_div").length == 0) {
        $(".td_controls_div.ison").removeClass("ison").removeAttr("style").find("a").hide();
    }
    if (eventTarget.closest(".ListTable tbody tr").length > 0) {
        if (eventTarget.is("[href-right]") || eventTarget.is("[href-info]") || !eventTarget.parents("tr").hasClass("tr_click_col")) {
            $(".ListTable tr ").removeClass("tr_click_col");
            eventTarget.parents("tr").addClass("tr_click_col");
        } else $(".ListTable tr ").removeClass("tr_click_col");


        //$(".td_controls_div.ison").removeClass("ison").removeAttr("style").find("a").hide();
    } else $(".ListTable tr ").removeClass("tr_click_col");

    if (eventTarget.closest("#Pagination").length == 0) {
        $("#input_page2").val($("#input_page2").attr("data-value"));
        $("#button2page_Box").hide();
    }

    //$(".ListTable tr ").fastClick(function () {
    //    if ($(this).hasClass("tr_click_col")) {
    //        $(".ListTable tr ").removeClass("tr_click_col");
    //    } else {
    //        $(".ListTable tr ").removeClass("tr_click_col");
    //        $(this).addClass("tr_click_col");
    //    }
    //})
    //$(".ListTable td [href-right],.ListTable td [href-info]").fastClick(function () {

    //    $(".ListTable tr ").removeClass("tr_click_col");
    //    $(this).parents("tr").addClass("tr_click_col");

    //})
}
function showGoback() {
    $("#bt_goBack").show();
}
////打开搜索
//function open_search() {
//    $("#search_opener").animate({ right: 0, opacity: 0 }, function () { $(this).hide(); });
//    parent.open_search($("#div_search_con"));
//}
////关闭搜索
//function close_search() {
//    if ($("#div_search_con .control-group").length > 0 && $("#div_search_con").html() != "")
//        $("#search_opener").show().css({ opacity: 0 }).animate({ right: 10, opacity: 1 });
//}
//给搜索表单赋值
function search_bind(v) {
    if (v.length > 0) {
        for (var i = 0; i < v.length; i++) {
            var w = $("[id*=" + v[i].id + "]");
            if (w.length == 0) w = $("#" + v[i].id.substring(accAdd(1, v[i].id.indexOf("_"))));
            if ((v[i].id).indexOf("_text_") > -1 || (v[i].id).indexOf("_select_") > -1) w.val(v[i].val);
            if ((v[i].id).indexOf("_radio_") > -1) w.find("[value=" + v[i].val + "]").attr("checked", "checked");
            //if ((v[i].id).indexOf("_check_") > -1) $("#" + v[i].id + "_" + v[i].val).attr("select", "select");
            if ((v[i].id).indexOf("_check_") > -1) $("[id$=" + v[i].id + "_" + v[i].val + "]").attr("checked", "true");
        }
       
        showSearch = false;
    }
}
function searchInputSelected(inputID) {
    $("#" + inputID).attr("selected", "selected");
}
function searchInputChecked(inputID) {
    $("#" + inputID).attr("checked", "checked");
}
//开始搜索
function searchStart(table_search_) {
    showSearch = true;
    $("#div_search").html("").append(table_search_.clone());
    $("[name='" + clientID + "$tb_currentPage']").val(0);

    fenghuolun_open($("#List")[0]);
    $("[data-id=bt_search]").click();
}





function Ahref2ListPage(href_) { parent.open_frameList(href_); return false; }
//初始化返回按钮事件
function default_bt_goBack() {
    //if ($("#bt_goBack").length > 0 && $("#bt_goBack").attr("href") != "") $("#bt_goBack").fastClick(function () {
    //    //location.href = $("#bt_goBack").attr("href");
    //    parent.open_frameList($("#bt_goBack").attr("href"));
    //    return false;
    //})
    //else $("#bt_goBack").hide();
}

function makeClientID() {
    if ($("#clientID").length > 0) clientID = $("#clientID").val();
    if (clientID != "") clientID += "";


}
function make_pageSize() {
    var auto_pageSize = 5;
    if (isMobile && $("#listCount").val() < 30) auto_pageSize = 10000000;
    else {
        if ($(".ListTable").length > 0) {
            var shengyu_height = parent.admin_list.height() - $("body").outerHeight(true) * 1 - 1 + $("[datakey]").length * trHeight;
            if (shengyu_height > 200) auto_pageSize = Math.floor(shengyu_height / trHeight);
            //alert("shengyu_height=" + shengyu_height + ",auto_pageSize=" + auto_pageSize);
        } else {
            var shengyu_height = parent.admin_list.height() - ($("body").outerHeight(true) - $("#ListShowUl").outerHeight(true));
            var col = Math.floor((bodyWidth - 1) / accAdd(liWidth, 1));
            if (shengyu_height > 150) auto_pageSize = col * Math.floor(shengyu_height / accAdd(liHeight, 1));
        }
    }
    $("[name='" + clientID + "$tb_pageSize']").val(auto_pageSize);
}
function checkbox_event() {
    if ($(".th_checkbox  input:checkbox").length > 0) $(".th_checkbox  input:checkbox").fastClick(function () { checkAll(this); });
    if ($(".ListTable td input:checkbox").length > 0) $(".ListTable td input:checkbox").fastClick(function () {
        if ($(this).is(":checked")) $(this).parents("tr").addClass("tr_hover_col");
        else $(this).parents("tr").removeClass("tr_hover_col");
        chk_bat();
    });
}
//构造排序DOM和事件
function orderBy_default() {
    if ($(".ListTable tr[datakey]").length == 0) return;
    if ($(".ListTable th[data-sort=1]").length > 0) $(".ListTable th[data-sort=1]").addClass("th_orderBy").fastClick(function () {
        $("[name='" + clientID + "$tb_currentPage']").val(0);
        var me = $(this);
        $("[name='" + clientID + "$tb_orderBy']").val(me.attr("data-column"));
        if (me.hasClass("desc")) {
            me.removeClass("desc").addClass("asc");
            $("[name='" + clientID + "$tb_descasc']").val("asc");
        } else {
            me.removeClass("asc").addClass("desc");
            $("[name='" + clientID + "$tb_descasc']").val("desc");
        }

        fenghuolun_open($(".ListTable tbody")[0]);
        $("[name='" + clientID + "$bt_orderBy']").click();
    });
    $(".ListTable th[data-sort=1]").removeClass("desc").removeClass("asc");
    //如何分辨是desc,asc
    if ($("[name='" + clientID + "$tb_orderBy']").val() != "") {
        var orderByval = $("[name='" + clientID + "$tb_orderBy']").val();
        $(".ListTable th[data-column='" + $("[name='" + clientID + "$tb_orderBy']").val() + "']").addClass($("[name='" + clientID + "$tb_descasc']").val());
    }
}
//初始化 修改状态 按钮 DOM+事件
function bt_state_default() {
    $(".state0").attr("title", "点击后启用");
    $(".state1").attr("title", "点击后禁用");
    if ($(".state0,.state1").length > 0) $(".state0,.state1").fastClick(function () {
        if (!$(this).hasClass("bt_img_hidden")) {
            $("[name='" + clientID + "$tb_id']").val($(this).parents("tr").attr("datakey"));
            $("[name='" + clientID + "$tb_state']").val($(this).hasClass("state0") ? "1" : "0");

            fenghuolun_open($(this).parents("tr"));
        }
        $("[name='" + clientID + "$bt_state']").click();
        return false;
    });
}
//初始化 上下移动 按钮 DOM+事件
function bt_move_default() {
    $(".moveUp,.moveDown").each(function () {
        if ($(this).parents("tr").find(".state0").length) $(this).addClass("bt_img_hidden");
    })
    if ($("[name='" + clientID + "$tb_currentPage']").val() == "0") $(".moveUp:not(.bt_img_hidden):first").addClass("bt_img_hidden");
    var allPage = Math.floor($("#listCount").val() / $("[name='" + clientID + "$tb_pageSize']").val());
    if ($("[name='" + clientID + "$tb_currentPage']").val() == allPage) $(".moveDown:not(.bt_img_hidden):last").addClass("bt_img_hidden");

    if ($(".moveUp,.moveDown").length > 0) $(".moveUp,.moveDown").fastClick(function () {
        if (!$(this).hasClass("bt_img_hidden")) {
            $("[name='" + clientID + "$tb_id']").val($(this).parents("tr").attr("datakey"));
            $("[name='" + clientID + "$tb_move']").val($(this).hasClass("moveUp") ? "1" : "-1");

            fenghuolun_open($(this).parents("tr"));
            $("[name='" + clientID + "$bt_move']").click();
        }
        return false;
    });
}
//分页参数设置
function page_build(fenghuolun_TrDatakey) {
    $("#ListShowUl li").width(liWidth).height(liHeight).show();
    $("#List").addClass("trH" + trHeight);
    if (fenghuolun_TrDatakey == "") fenghuolun_open_($("#List")[0], 0.3, true);
    else if (fenghuolun_TrDatakey != null) {
        var idA = fenghuolun_TrDatakey.split(",");
        for (var i = 0; i < idA.length; i++) fenghuolun_open($("[datakey][datakey='" + idA[i] + "']"));

    }
    //return;
    if ($("#Pagination").length > 0) {
        $("#Pagination").pagination($("#listCount").val(), {
            callback: pageselectCallback,
            prev_text: "上一页",
            next_text: "下一页",
            items_per_page: $("[name='" + clientID + "$tb_pageSize']").val(),
            num_display_entries: 0,
            current_page: $("[name='" + clientID + "$tb_currentPage']").val(),
            num_edge_entries: 0
        });

    }
}
function page_build_() {

    orderBy_default();
    bt_state_default();
    bt_move_default();
    checkbox_event();
    chk_bat();
    //全局控件
    //if (Element_InRight) $(Element_InRight).show();
    //else $(".ListControl").show();
    //if (IsShowInfoOpener) $(".td_info,.td_info .ListControl").show();//infoOpener控件
    //
    if ($("#listCount").val() == 0) {
        $(".ListTable,#Pagination,#Bottom_Control_Box").hide();
        $("#listTable_null").show();
        $("#ListCountMsg").hide();
    }
    else {
        $(".ListTable,#Pagination,#Bottom_Control_Box").show();
        $("#listTable_null").hide();
        $("#ListCountMsg").show();
        //
        if ($("#Bottom_Control_Box").find(".ListControl:visible").length > 0) $(".ListTable .ListControl").show();
    }
    //隐藏行中的某个元素
    $("[hideRow]").each(function () {
        $(this).parents("tr").find($(this).attr("hideRow")).hide();
    })
    $("[hidePublic]").each(function () {
        $($(this).attr("hidePublic")).hide();
    })
    $("[removeRow]").each(function () {
        $(this).parents("tr").find($(this).attr("removeRow")).remove();
    })
    $("[removePublic]").each(function () {
        $($(this).attr("removePublic")).remove();
    })
    try { after_pageRebuild(); }//页面自定义函数
    catch (e) { }
    window.parent.resizeDOM_index();
    //if ($("#listCount").val() == $("#pageSize").val()) $("#Pagination").hide();
    //else $("#Pagination").show();
    $(".td_controls_div").each(function (i, w) {
        var me = $(w);
        var meChildrenWidth = 0;
        me.find("a").each(function (t, w_) {
            meChildrenWidth += $(w_).outerWidth(true) * 1;
        })
        if (meChildrenWidth > me.width()) me.addClass("isMobile");
    })
    $(".td_controls_div.isMobile a").hide();
    //$(".td_controls_div_kaiguan").click(function () {
    //    var me = $(this), meP = me.parent();
    //    meP.addClass("ison");
    //    var mePwidth = 0;
    //    var mePa=meP.find("a");
    //    for (var i = 0; i < mePa.length ; i++) {
    //        var thisa = $(mePa[i]);
    //        thisa.show();
    //        if (mePwidth < thisa.width()) mePwidth = thisa.width();
    //    }
    //    meP.width(mePwidth).height(mePa.length * 32);
    //    meP.css({ marginTop:-meP.outerHeight(true)/2 })
    //})

    $(".td_controls_div_kaiguan").fastClick(function (event) {
        var me = $(event.target), meP = me.parent();
        meP.addClass("ison");
        var mePwidth = 0;
        var mePa = meP.find("a");
        for (var i = 0; i < mePa.length ; i++) {
            var thisa = $(mePa[i]);
            thisa.show();
            if (mePwidth < thisa.width()) mePwidth = thisa.width();

        }
        var height = mePa.length * 32;
        meP.width(mePwidth);
        //var  marginTop=-meP.outerHeight(true) / 2;
        //meP.css({ marginTop: -meP.outerHeight(true) / 2 })
        meP.animate({ height: height, marginTop: -height / 2 }, "fast", function () { });
        return false;

    })

    $(".ListTable td [href-right],.ListTable td [href-info]").fastClick(function () {

        $(".ListTable tr ").removeClass("tr_click_col");
        $(this).parents("tr").addClass("tr_click_col");

    })


    parent.frame_list_ok(showSearch, $("#listCount").val());
    $(".list_pic[data-width]").fastClick(function () { parent.Pic_Item_Click(this); });
    //$('.td_info .control').popover({ title: "a", content: "<iframe src=\"/sys/setting/default.aspx\"></iframe>",selector:parent.document.getElementById("kkk"),placement:"left" });
    var _listCount = $("#listCount").val() * 1, _currentPage = $("[name='" + clientID + "$tb_currentPage']").val() * 1, _pageSize = $("[name='" + clientID + "$tb_pageSize']").val() * 1;
    var _NumberFrom = 0;
    if (_listCount > 0) _NumberFrom = accAdd(_currentPage * _pageSize, 1);
    var _NumberTo = accAdd(_NumberFrom, _pageSize) - 1;
    if (_NumberTo > _listCount) _NumberTo = _listCount;
    $("#ListCountMsg_Number").html(_listCount);
    $("#ListCountMsg_NumberFrom").html(_NumberFrom);
    $("#ListCountMsg_NumberTo").html(_NumberTo);
    $("#ListCountMsg_PageSize").html(_pageSize);
    $("#ListCountMsg_NumberPage").html(Math.ceil(_listCount / _pageSize));

    $("#input_page2").val($("#input_page2").attr("data-value"));
}
//翻页事件
function pageselectCallback(page_id, jq) {
    //if ($(".fenghuolun:visible").length == 0) fenghuolun_open_($(".ListTable tbody")[0], 0.3, true);
    make_pageSize();
    //alert(page_id); //回调函数，进一步使用请参阅说明文档
    //$("body").append("<b>" + page_id + "</b>");
    //if ($("#listCount").val() > 0) {
    fenghuolun_open($("#List")[0]);
    $("[name='" + clientID + "$tb_currentPage']").val(page_id);
    $("[name='" + clientID + "$bt_page']").click();
    //}
    return false;
}
function page_bind_ok(listSumColumn) {
    $("#ListShowUl li").width(liWidth).height(liHeight).show();
    $("#List").addClass("trH" + trHeight);
    if ($("#listCount").val() != "") {
        $("#ListMsg_Count").show().find("b").html($("#listCount").val());
    }
    bindEvent_href();
    page_build_();
    fenghuolun_closeALL();
    chkvalue_4hide();
    // alert(listSumColumn.length);
    for (var i = 0; i < listSumColumn.length; i++) {
        $(".ListTable th[data-sum=1][data-column=" + listSumColumn[i].Name + "]").append("<div class=\"thCon_sum\">合:" + listSumColumn[i].SumResult + "</div>")
    }
}
function page_bind_SumColum(listSumColumn) {
    ListSumColumn = listSumColumn;
}
function checkAll(w_) {
    var w = $(w_);
    if (w.is(":checked")) $(".td_checkbox :checkbox:visible").attr("checked", true).parents("tr").addClass("tr_hover_col");
    else $(".td_checkbox :checkbox:visible").attr("checked", false).parents("tr").removeClass("tr_hover_col");
    chk_bat();
}

function chk_bat() {
    if ($(".ListTable input:checkbox:checked").length > 0) $(".button_bat").removeAttr("disabled").removeClass("button_disabled");
    else $(".button_bat").attr("disabled", "disabled").addClass("button_disabled");
    var checkedIDstr = "";
    $(".ListTable td input:checkbox:checked").each(function () {
        if (checkedIDstr != "") checkedIDstr += ",";
        checkedIDstr += $(this).parents("tr").attr("datakey");
    })
    $("[name='" + clientID + "$tb_batIDstr']").val(checkedIDstr);
}
//
//判断是否点击了确定按钮
function chk_confirm(w_, str) {
    if (w_.disabled) return false;
    else {
        var msgStr = "";
        if (str) msgStr = str;
        else msgStr = $(w_).attr("data-confirm-msg");
        if (!msgStr || msgStr == undefined || msgStr == "") return true;
        else return confirm(msgStr);
    }
}
//响应父页面banner中的bt请求
function bt_add_click() {
    $("#button_add").click();
}
function bt_del_click() {
    button_bat_del_click();
    fenghuolun_open($(".ListTable td input:checkbox:checked").parents("tr"));
}
function button_bat_del_click() {
    if (chk_confirm($(".btn_bat_del"), $(".btn_bat_del").attr("alertmsg"))) { $("[name='" + clientID + "$bt_bat']").click(); }
}
function insertImgCkeditor(imgsrc) {
    CKEDITOR.instances.content.insertHtml("<img src=\"" + imgsrc + "\"/>");
}

//点击批量按钮
function button_bat_click(w_) {
    if (chk_confirm(w_)) {
        var w = $(w_);
        if (w.is("[data-action]")) {
            $("[name='" + clientID + "$tb_batAction']").val($(w_).attr("data-action"));
            $("[name='" + clientID + "$bt_bat']").click();
        } else if (w.is("[data-href-right]")) {
            var href = w.attr("data-href-right");
            if (href.indexOf("?") > 0) href += "&";
            else href += "?";
            href += "idStr=" + $("[name='" + clientID + "$tb_batIDstr']").val();
            parent.open_frameRight(getAbsoluteUrl(href), w.attr("data-title"));
        }
    }
}
//发消息
function NewMessage() {
    var idA = $("[name$='tb_batIDstr']").val().split(",");
    var ReceiveIDStr = "";
    for (var i = 0; i < idA.length; i++) {
        var thisUserID = $("[datakey=" + idA[i] + "]").find("[data-userid]").attr("data-userid");
        ReceiveIDStr += (ReceiveIDStr == "" ? "" : ",") + thisUserID;
    }
    parent.open_frameRight(getAbsoluteUrl("/sys/message/form.aspx?ReceiveIDStr=" + ReceiveIDStr), "发消息"); return false;
}

function EnterPress(e){ //传入 event
var e = e || window.event;
if(e.keyCode == 13){
    $("#Button1").click();
}
} 